//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.member;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.MemberChangeCardDao;
import com.aidong.dao.wills.MemberChangeShopDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberStopedCardDao;
import com.aidong.dao.wills.MemberTransferDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.MemberCard;
import com.aidong.model.wills.MemberChangeCard;
import com.aidong.model.wills.MemberChangeShop;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.MemberStopedCard;
import com.aidong.model.wills.PayList;
import com.aidong.web.controller.member.PayController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"memberChange"})
public class MemberChangeController extends PayController {
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    MemberStopedCardDao memberStopedCardDao;
    @Resource
    MemberChangeShopDao memberChangeShopDao;
    @Resource
    MemberChangeCardDao memberChangeCardDao;
    @Resource
    MemberTransferDao memberTransferDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;

    public MemberChangeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("member/change/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "saleType",required = false) String saleType, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.contractCode", contractCode);
        cd.eq("c.memberCode", memberCode);
        if(!this.chkNull(dateFrom)) {
            cd.ge("c.saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("c.saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        if("1".equals(saleType)) {
            this.memberStopedCardDao.queryPageList(pg, cd);
        } else if("2".equals(saleType)) {
            this.memberChangeShopDao.queryPageList(pg, cd);
        } else if("3".equals(saleType)) {
            this.memberChangeCardDao.queryPageList(pg, cd);
        }

        ModelAndView mv = new ModelAndView("member/change/_list" + saleType);
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("member/change/add");
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_stoped"})
    @ResponseBody
    public String _stoped(@ModelAttribute("contract") MemberStopedCard stopedCard, @RequestParam("payls") String payls) {
        if(this.chkNull(stopedCard.getContractCode())) {
            return this.getResult(false, "请输入合同号");
        } else if(this.chkNull(stopedCard.getBeginStopTime())) {
            return this.getResult(false, "请输入开始停卡日期");
        } else if(this.chkNull(stopedCard.getEndStopTime())) {
            return this.getResult(false, "请输入结束停卡日期");
        } else if(this.chkNullZ(stopedCard.getStopMonth())) {
            return this.getResult(false, "请输入停卡月数");
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(stopedCard.getBeginStopTime());
            cal.add(2, stopedCard.getStopMonth().intValue());
            stopedCard.setEndStopTime(cal.getTime());
            MemberContract contract = this.memberContractDao.getByContractCode(stopedCard.getContractCode());
            if(contract == null) {
                return this.getResult(false, "合同号错误");
            } else {
                Date beginTime = contract.getBeginTime();
                Date endTime = contract.getEndTime();
                if(beginTime != null && endTime != null && contract.getStatus().intValue() == 1 && contract.getEffectiveDate() == null && !"8".equals(contract.getSaleType())) {
                    if(endTime.getTime() < System.currentTimeMillis()) {
                        return this.getResult(false, "合同已过有效期,不能停卡");
                    } else {
                        double leftAmount;
                        if(contract.getTotalCount().intValue() > 0) {
                            leftAmount = (double)Math.round((double)contract.getRestCount().intValue() * contract.getFactPrice().doubleValue() / (double)contract.getTotalCount().intValue());
                        } else if(beginTime.getTime() > System.currentTimeMillis()) {
                            leftAmount = contract.getFactPrice().doubleValue();
                        } else {
                            leftAmount = (double)Math.round((double)(endTime.getTime() - System.currentTimeMillis()) * contract.getFactPrice().doubleValue() / (double)(endTime.getTime() - beginTime.getTime()));
                        }

                        if(leftAmount > contract.getFactPrice().doubleValue()) {
                            leftAmount = contract.getFactPrice().doubleValue();
                        }

                        if(leftAmount <= 0.0D) {
                            leftAmount = 0.0D;
                        }

                        if(!this.chkNullZ(stopedCard.getEarnestId())) {
                            MemberEarnest payList = (MemberEarnest)this.memberEarnestDao.getById(stopedCard.getEarnestId());
                            if(payList == null || payList.getStatus().intValue() != 0 || !payList.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                return this.getResult(false, "定金不可用");
                            }

                            stopedCard.setEarnestAmount(payList.getEarnestAmount());
                        } else {
                            stopedCard.setEarnestId((Integer)null);
                            stopedCard.setEarnestAmount(Double.valueOf(0.0D));
                        }

                        stopedCard.setShopCode(this.getLoginShopCode());
                        stopedCard.setSaleDate(new Date());
                        stopedCard.setCardCode(contract.getCardCode());
                        stopedCard.setMemberCode(contract.getMemberCode());
                        stopedCard.setBeginTime(contract.getBeginTime());
                        stopedCard.setEndTime(contract.getEndTime());
                        stopedCard.setNewEndTime(contract.getNewEndTime());
                        stopedCard.setTotalCount(contract.getTotalCount());
                        stopedCard.setRestCount(contract.getRestCount());
                        stopedCard.setOutAmount(Double.valueOf(leftAmount));
                        stopedCard.setCardType(contract.getCardType());
                        stopedCard.setManyShop(contract.getManyShop());
                        stopedCard.setStopFee(contract.getStopFee());
                        stopedCard.setChangeFee(contract.getChangeFee());
                        stopedCard.setMendFee(contract.getMendFee());
                        stopedCard.setChgShopFee(contract.getChgShopFee());
                        stopedCard.setContDiscount(contract.getContDiscount());
                        stopedCard.setAddFee(Double.valueOf(this.roundMoney(contract.getStopFee().doubleValue() * (double)stopedCard.getStopMonth().intValue())));
                        stopedCard.setPayAmount(Double.valueOf(stopedCard.getAddFee().doubleValue() - stopedCard.getEarnestAmount().doubleValue()));
                        if(stopedCard.getPayAmount().doubleValue() < 0.0D) {
                            stopedCard.setPayAmount(Double.valueOf(0.0D));
                        }

                        stopedCard.setStatus(Integer.valueOf(1));
                        stopedCard.setSignStatus(Integer.valueOf(this.chkNullZ(contract.getSignStatus())?2:0));
                        stopedCard.setPrintLabel(Integer.valueOf(0));
                        stopedCard.setCreateUser(this.getLoginUserCode());
                        stopedCard.setCreateTime(new Date());
                        stopedCard.setUpdateTime(new Date());
                        ArrayList payList1 = new ArrayList();
                        StringBuilder payMsg = new StringBuilder();
                        PayList pay = this.getPayList(payls, payList1, "05", payMsg);
                        if(pay == null) {
                            return this.getResult(false, payMsg.toString());
                        } else if(!this.equals(pay.getAmount().doubleValue(), stopedCard.getPayAmount().doubleValue())) {
                            return this.getResult(false, "付款金额不一致");
                        } else {
                            stopedCard.setSalesCode(pay.getSalesCode());
                            stopedCard.setSalesCode2(pay.getSalesCode2());

                            try {
                                this.memberStopedCardDao.addStopedCard(stopedCard, payList1);
                                this.smsListService.addSmsList("14", stopedCard.getSaleId().intValue());
                                this.addUserLog("会员停卡", "合同号:" + stopedCard.getContractCode() + ",会员:" + stopedCard.getMemberCode());
                            } catch (Exception var13) {
                                this.log.error("add stopedcard", var13);
                                return this.getResult(false, "保存发生错误");
                            }

                            return this.getSuccess(String.valueOf(stopedCard.getSaleId()), "保存完成");
                        }
                    }
                } else {
                    return this.getResult(false, "无效合同");
                }
            }
        }
    }

    @RequestMapping({"_chgShop"})
    @ResponseBody
    public String _chgShop(@ModelAttribute("contract") MemberChangeShop changeShop, @RequestParam("payls") String payls) {
        if(this.chkNull(changeShop.getContractCode())) {
            return this.getResult(false, "请输入合同号");
        } else if(this.chkNull(changeShop.getNewShopCode())) {
            return this.getResult(false, "请输入转入门店");
        } else {
            MemberContract contract = this.memberContractDao.getByContractCode(changeShop.getContractCode());
            if(contract == null) {
                return this.getResult(false, "合同号错误");
            } else {
                Date beginTime = contract.getBeginTime();
                Date endTime = contract.getEndTime();
                if(beginTime != null && endTime != null && contract.getStatus().intValue() == 1 && contract.getEffectiveDate() == null && !"8".equals(contract.getSaleType())) {
                    if(endTime.getTime() < System.currentTimeMillis()) {
                        return this.getResult(false, "合同已过有效期,不能转卡");
                    } else {
                        double leftAmount;
                        if(contract.getTotalCount().intValue() > 0) {
                            leftAmount = (double)Math.round((double)contract.getRestCount().intValue() * contract.getFactPrice().doubleValue() / (double)contract.getTotalCount().intValue());
                        } else if(beginTime.getTime() > System.currentTimeMillis()) {
                            leftAmount = contract.getFactPrice().doubleValue();
                        } else {
                            leftAmount = (double)Math.round((double)(endTime.getTime() - System.currentTimeMillis()) * contract.getFactPrice().doubleValue() / (double)(endTime.getTime() - beginTime.getTime()));
                        }

                        if(leftAmount > contract.getFactPrice().doubleValue()) {
                            leftAmount = contract.getFactPrice().doubleValue();
                        }

                        if(leftAmount <= 0.0D) {
                            leftAmount = 0.0D;
                        }

                        if(contract.getShopCode().equalsIgnoreCase(changeShop.getNewShopCode())) {
                            return this.getResult(false, "转入门店不能和原门店相同");
                        } else {
                            DicShopCode shop = (DicShopCode)this.dicShopCodeDao.getById(changeShop.getNewShopCode());
                            if(shop != null && shop.getStatus().intValue() == 1) {
                                if(!this.chkNullZ(changeShop.getEarnestId())) {
                                    MemberEarnest payList = (MemberEarnest)this.memberEarnestDao.getById(changeShop.getEarnestId());
                                    if(payList == null || payList.getStatus().intValue() != 0 || !payList.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                        return this.getResult(false, "定金不可用");
                                    }

                                    changeShop.setEarnestAmount(payList.getEarnestAmount());
                                } else {
                                    changeShop.setEarnestId((Integer)null);
                                    changeShop.setEarnestAmount(Double.valueOf(0.0D));
                                }

                                changeShop.setSaleDate(new Date());
                                changeShop.setShopCode(this.getLoginShopCode());
                                changeShop.setContractCode(contract.getContractCode());
                                changeShop.setNewShopCode(shop.getCode());
                                changeShop.setCardCode(contract.getCardCode());
                                changeShop.setMemberCode(contract.getMemberCode());
                                changeShop.setAddFee(contract.getChgShopFee());
                                changeShop.setPayAmount(Double.valueOf(changeShop.getAddFee().doubleValue() - changeShop.getEarnestAmount().doubleValue()));
                                if(changeShop.getPayAmount().doubleValue() < 0.0D) {
                                    changeShop.setPayAmount(Double.valueOf(0.0D));
                                }

                                changeShop.setStatus(Integer.valueOf(1));
                                changeShop.setSignStatus(Integer.valueOf(this.chkNullZ(contract.getSignStatus())?2:0));
                                changeShop.setPrintLabel(Integer.valueOf(0));
                                changeShop.setCreateUser(this.getLoginUserCode());
                                changeShop.setCreateTime(new Date());
                                changeShop.setUpdateTime(new Date());
                                ArrayList payList1 = new ArrayList();
                                StringBuilder payMsg = new StringBuilder();
                                PayList pay = this.getPayList(payls, payList1, "09", payMsg);
                                if(pay == null) {
                                    return this.getResult(false, payMsg.toString());
                                } else if(!this.equals(pay.getAmount().doubleValue(), changeShop.getPayAmount().doubleValue())) {
                                    return this.getResult(false, "付款金额不一致");
                                } else {
                                    changeShop.setSalesCode(pay.getSalesCode());
                                    changeShop.setSalesCode2(pay.getSalesCode2());
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                    contract.setSaleId((Integer)null);
                                    contract.setShopCode(changeShop.getNewShopCode());
                                    contract.setContractCode((String)null);
                                    contract.setSaleType("5");
                                    contract.setSaleDate(new Date());
                                    contract.setPromotionId((Integer)null);
                                    contract.setEarnestId((Integer)null);
                                    contract.setEarnestAmount(Double.valueOf(0.0D));
                                    contract.setBeginTime(this.parseDate(sdf.format(new Date()), sdf));
                                    contract.setCardPrice(Double.valueOf(leftAmount));
                                    contract.setTotalPrice(Double.valueOf(leftAmount));
                                    contract.setFactPrice(Double.valueOf(leftAmount));
                                    contract.setPayAmount(Double.valueOf(0.0D));
                                    contract.setInAmount(Double.valueOf(leftAmount));
                                    contract.setOldContractCode(changeShop.getContractCode());
                                    contract.setCommissionDate(new Date());
                                    contract.setCreateTime(new Date());
                                    contract.setUpdateTime(new Date());
                                    contract.setReviewStatus(Integer.valueOf(0));
                                    contract.setGiftNote((String)null);

                                    try {
                                        this.memberChangeShopDao.addChangeShop(changeShop, payList1, contract);
                                        this.smsListService.addSmsList("16", changeShop.getSaleId().intValue());
                                        this.addUserLog("会员转店", "合同号:" + contract.getContractCode() + ",会员:" + contract.getMemberCode());
                                    } catch (Exception var14) {
                                        this.log.error("add changeshop", var14);
                                        return this.getResult(false, "保存发生错误");
                                    }

                                    return this.getSuccess(String.valueOf(changeShop.getSaleId()), "保存完成");
                                }
                            } else {
                                return this.getResult(false, "转入门店错误");
                            }
                        }
                    }
                } else {
                    return this.getResult(false, "无效合同");
                }
            }
        }
    }

    @RequestMapping({"_chgCard"})
    @ResponseBody
    public String _chgCard(@ModelAttribute("contract") MemberChangeCard changeCard, @RequestParam("payls") String payls) {
        if(this.chkNull(changeCard.getCardCode())) {
            return this.getResult(false, "请输入卡号");
        } else if(this.chkNull(changeCard.getNewCardCode())) {
            return this.getResult(false, "请输入新卡号");
        } else {
            MemberInfo member = this.memberInfoDao.getByCardCode(changeCard.getCardCode());
            if(member == null) {
                return this.getResult(false, "会员卡号错误");
            } else {
                MemberContract contract = this.memberContractDao.getCurrentByMemberCode(member.getMemberCode());
                if(contract == null) {
                    contract = this.memberContractDao.getByMemberCode(member.getMemberCode());
                }

                if(contract == null) {
                    return this.getResult(false, "会员无有效合同");
                } else {
                    Date beginTime = contract.getBeginTime();
                    Date endTime = contract.getEndTime();
                    if(beginTime != null && endTime != null && contract.getStatus().intValue() == 1 && contract.getEffectiveDate() == null && !"8".equals(contract.getSaleType())) {
                        if(contract.getNewEndTime() != null) {
                            endTime = contract.getNewEndTime();
                        }

                        if(endTime.getTime() < System.currentTimeMillis()) {
                            return this.getResult(false, "会员合同已过有效期");
                        } else {
                            MemberCard card = this.memberCardDao.getById(changeCard.getNewCardCode());
                            if(card != null && card.getShopCode().equalsIgnoreCase(this.getLoginShopCode()) && card.getStatus().intValue() == 1) {
                                if(!this.chkNullZ(changeCard.getEarnestId())) {
                                    MemberEarnest payList = (MemberEarnest)this.memberEarnestDao.getById(changeCard.getEarnestId());
                                    if(payList == null || payList.getStatus().intValue() != 0 || !payList.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                        return this.getResult(false, "定金不可用");
                                    }

                                    changeCard.setEarnestAmount(payList.getEarnestAmount());
                                } else {
                                    changeCard.setEarnestId((Integer)null);
                                    changeCard.setEarnestAmount(Double.valueOf(0.0D));
                                }

                                changeCard.setContractCode(contract.getContractCode());
                                changeCard.setShopCode(this.getLoginShopCode());
                                changeCard.setSaleDate(new Date());
                                changeCard.setMemberCode(member.getMemberCode());
                                changeCard.setAddFee(contract.getMendFee());
                                changeCard.setPayAmount(Double.valueOf(changeCard.getAddFee().doubleValue() - changeCard.getEarnestAmount().doubleValue()));
                                if(changeCard.getPayAmount().doubleValue() < 0.0D) {
                                    changeCard.setPayAmount(Double.valueOf(0.0D));
                                }

                                changeCard.setStatus(Integer.valueOf(1));
                                changeCard.setSignStatus(Integer.valueOf(this.chkNullZ(contract.getSignStatus())?2:0));
                                changeCard.setPrintLabel(Integer.valueOf(0));
                                changeCard.setCreateUser(this.getLoginUserCode());
                                changeCard.setCreateTime(new Date());
                                changeCard.setUpdateTime(new Date());
                                ArrayList payList1 = new ArrayList();
                                StringBuilder payMsg = new StringBuilder();
                                PayList pay = this.getPayList(payls, payList1, "07", payMsg);
                                if(pay == null) {
                                    return this.getResult(false, payMsg.toString());
                                } else if(!this.equals(pay.getAmount().doubleValue(), changeCard.getPayAmount().doubleValue())) {
                                    return this.getResult(false, "付款金额不一致");
                                } else {
                                    changeCard.setSalesCode(pay.getSalesCode());
                                    changeCard.setSalesCode2(pay.getSalesCode2());

                                    try {
                                        this.memberChangeCardDao.addChangeCard(changeCard, payList1);
                                        this.smsListService.addSmsList("15", changeCard.getSaleId().intValue());
                                        this.addUserLog("会员补卡", "合同号:" + changeCard.getContractCode() + ",会员:" + changeCard.getMemberCode());
                                    } catch (Exception var12) {
                                        this.log.error("add changecard", var12);
                                        return this.getResult(false, "保存发生错误");
                                    }

                                    return this.getSuccess(String.valueOf(changeCard.getSaleId()), "保存完成");
                                }
                            } else {
                                return this.getResult(false, "新卡号错误");
                            }
                        }
                    } else {
                        return this.getResult(false, "会员无效合同");
                    }
                }
            }
        }
    }
}
